<style type="text/css">
table
{
	width:100%;
	text-align:center;
	border-spacing:0px;
	border-left:1px solid black;
	border-top:1px solid black;
}
td
{
	border-bottom:1px solid black;
	border-right:1px solid black;
}
</style>
<script src="../js/jquery-1.10.0.js">
</script>
<?php
class getForm
{
	public function __construct()
	{
		include("../app/connection.php");
	}
	public function convert($key)
	{
		$re="";
		if($key=="MSSV")$re="Mã Số Sinh Viên";
		else if($key=="MAGIAOVIEN")$re="Mã Giáo Viên";
		else if($key=="MAKHOA")$re="Mã Khoa";
		else if($key=="TENKHOA")$re="Tên Khoa";
		else if($key=="HOTEN")$re="Họ Tên";
		else if($key=="GIOITINH")$re="Giới Tính";
		else if($key=="NGAYSINH")$re="Ngày Sinh";
		else if($key=="MAKHOA")$re="Mã Khoa";
		else if($key=="PASSWORD")$re="PASSWORD";
		else if($key=="TRANGTHAI")$re="Trạng Thái";
		else if($key=="KHOAHOC")$re="Khoá";
		else if($key=="MAMONHOC")$re="Mã Môn Học";
		else if($key=="TENMONHOC")$re="Tên Môn Học";
		else if($key=="TINCHILYTHUYET")$re="Tín Chỉ Lý Thuyết";
		else if($key=="TINCHITHUCHANH")$re="Tín Chỉ Thực Hành";
		else if($key=="MAQUANTRI")$re="Mã Quản Trị";
		else if($key=="HOTEN")$re="Họ Tên";
		else if($key=="HOCKY")$re="Học Kỳ";
		else if($key=="NAM")$re="Năm";
		else if($key=="NGAYBATDAU")$re="Ngày Bắt Đầu";
		else if($key=="NGAYKETTHUC")$re="Ngày Kết Thúc";
		else if($key=="NGAYMODANGKY")$re="Ngày Mở Đăng Ký";
		else if($key=="NGAYDONGDANGKY")$re="Ngày Đóng Đăng Ký";
		else if($key=="MALOP")$re="Mã Lớp";
		else if($key=="MALOAI")$re="Mã Loại";
		else if($key=="THU")$re="Thứ";
		else if($key=="CA")$re="Ca";
		else if($key=="PHONG")$re="Phòng";
		else if($key=="TOIDA")$re="Tối Đa";
		else if($key=="TINCHILYTHUYET")$re="Tín Chỉ Lý Thuyết";
		else if($key=="TINCHITHUCHANH")$re="Tín Chỉ Thực Hành";
		else if($key=="DIEMTHUCHANH")$re="Điểm Thực Hành";
		else if($key=="DIEMCUOIKY")$re="Điểm Cuối Kỳ";
		else if($key=="DIEMGIUAKY")$re="Điểm Giữa Kỳ";
		else if($key=="DIEMTRUNGBINH")$re="Điểm Trung Bình";
		else $re="Chưa Xác Định";
		return $re;
	}
	public function returnForm($type)
	{
		$table="";
		if($type=="student")$table="SINHVIEN";
		else if($type=="teacher")$table="GIAOVIEN";
		else if($type=="subject")$table="MONHOC";
		else if($type=="department")$table="KHOA";
		else if($type=="manager")$table="QUANTRIVIEN";
		else if($type=="semeter")$table="HOCKY";
		else if($type=="class")$table="LOP";
		else if($type=="register")$table="DANGKY";
		else if($type=="result")$table="KETQUA";
		echo "<input type='hidden' value='$table' id='table'>";
		$result = mysql_query("select * from $table");
		echo "<table><tr>";
		$i=0;
		while($col = mysql_fetch_field($result))
		{
			$colTitle=self::convert($col->name);
			echo "<td>";
			echo "<input type='checkbox' name='col[]' value='$col->name' checked>$colTitle<br>";
			$filter = mysql_query("select distinct $col->name from $table");
			echo "<select name='select_$col->name'>";
			echo "<option value=''></option>";
			while($r= mysql_fetch_array($filter))
			{
				echo "<option value='$r[0]'>$r[0]</option>";
			}
			echo "</select>";
			echo "</td>";
			if($type=="class"&&$i==2)
			{
				echo "<td><input type='checkbox' name='col[]' value='HOTEN' checked>Tên Giáo Viên<br>";
				$re=mysql_query("select distinct HOTEN from GIAOVIEN");
				echo "<select name='select_HOTEN'>";
				echo "<option value=''></option>";
				while($r= mysql_fetch_array($re))
				{
					echo "<option value='$r[0]'>$r[0]</option>";
				}
				echo "</select>";
				echo "</td>";
			}
			if($type=="class"&&$i==1)
			{
				echo "<td><input type='checkbox' name='col[]' value='TENMONHOC' checked>Tên Môn Học<br>";
				$re=mysql_query("select distinct TENMONHOC from MONHOC");
				echo "<select name='select_TENMONHOC'>";
				echo "<option value=''></option>";
				while($r= mysql_fetch_array($re))
				{
					echo "<option value='$r[0]'>$r[0]</option>";
				}
				echo "</select>";
				echo "</td>";
			}
			if(($type=="register"||$type=="result")&&$i==0)
			{
				echo "<td><input type='checkbox' name='col[]' value='HOTEN' checked>Tên Sinh Viên<br>";
				$re=mysql_query("select distinct HOTEN from SINHVIEN");
				echo "<select name='select_HOTEN'>";
				echo "<option value=''></option>";
				while($r= mysql_fetch_array($re))
				{
					echo "<option value='$r[0]'>$r[0]</option>";
				}
				echo "</select>";
				echo "</td>";
				if($type=="result")
				{
					echo "<td><input type='checkbox' name='col[]' value='MAKHOA' checked>Mã Khoa<br>";
					$re=mysql_query("select distinct MAKHOA from KHOA");
					echo "<select name='select_MAKHOA'>";
					echo "<option value=''></option>";
					while($r= mysql_fetch_array($re))
					{
						echo "<option value='$r[0]'>$r[0]</option>";
					}
					echo "</select>";
					echo "</td>";
				}
			}
			if(($type=="student"||$type=="teacher"||$type=="subject")&&$i==4)
			{
				echo "<td><input type='checkbox' name='col[]' value='TENKHOA' checked>Tên Khoa<br>";
				$re=mysql_query("select distinct TENKHOA from KHOA");
				echo "<select name='select_TENKHOA'>";
				echo "<option value=''></option>";
				while($r= mysql_fetch_array($re))
				{
					echo "<option value='$r[0]'>$r[0]</option>";
				}
				echo "</select>";
				echo "</td>";
			}
			$i++;
		}
		echo "</tr></table>";
	}
	public function returnQuery($table,$checkboxArr,$checkedArr,$selectedArr)
	{
		$query ="SELECT ";
		if($table=="SINHVIEN"||$table=="GIAOVIEN"||$table=="MONHOC")
		{
			foreach($checkedArr as $col)
			{
				if($col!="MAKHOA")
				{
					$query=$query.$col.",";
				}
				else
				{
					$query=$query."KHOA.MAKHOA".",";
				}
			}
			$query = substr($query,0,strlen($query)-1);
			$query = $query." From $table,KHOA where ";
			for($i=0;$i<count($checkboxArr);$i++)
			{
				if($checkboxArr[$i]!="MAKHOA")
				{
					$query=$query.$checkboxArr[$i]." like '%$selectedArr[$i]%' and ";
				}
				else
				{
					$query=$query."KHOA.MAKHOA"." like '%$selectedArr[$i]%' and ";
				}
			}
			$query = $query."KHOA.MAKHOA = $table.MAKHOA";
		}
		else if($table=="LOP")
		{
			foreach($checkedArr as $col)
			{
				if($col=="HOCKY")
				{
						$query=$query."LOP.HOCKY".",";
				}
				else if($col=="MAM")
				{
						$query=$query."LOP.MAM".",";
				}
				else if($col=="MAMONHOC")
				{
						$query=$query."MONHOC.MAMONHOC".",";
				}
				else if($col=="TRANGTHAI")
				{
						$query=$query."LOP.TRANGTHAI".",";
				}
				else if($col!="MAGIAOVIEN")
				{
					$query=$query.$col.",";
				}
				else
				{
					if($col=="MAGIAOVIEN")
					{
						$query=$query."GIAOVIEN.MAGIAOVIEN".",";
					} 
				}
			}
			$query = substr($query,0,strlen($query)-1);
			$query = $query." From $table,GIAOVIEN,MONHOC where ";
			for($i=0;$i<count($checkboxArr);$i++)
			{
				if($checkboxArr[$i]=="HOCKY")
				{
					$query=$query."LOP.HOCKY like '%$selectedArr[$i]%' and ";
				}
				else if($checkboxArr[$i]=="MAM")
				{
					$query=$query."LOP.MAM like '%$selectedArr[$i]%' and ";
				}
				else if($checkboxArr[$i]=="MAMONHOC")
				{
					$query=$query."LOP.MAMONHOC like '%$selectedArr[$i]%' and ";
				}
				else if($checkboxArr[$i]=="TRANGTHAI")
				{
					$query=$query."LOP.TRANGTHAI like '%$selectedArr[$i]%' and ";
				}
				else if($checkboxArr[$i]!="MAGIAOVIEN")
				{
					$query=$query.$checkboxArr[$i]." like '%$selectedArr[$i]%' and ";
				}
				else
				{
					$query=$query."GIAOVIEN.MAGIAOVIEN"." like '%$selectedArr[$i]%' and ";
				}
			}
			$query = $query."GIAOVIEN.MAGIAOVIEN = $table.MAGIAOVIEN and MONHOC.MAMONHOC=$table.MAMONHOC";
		}
		else if($table=="DANGKY")
		{
			foreach($checkedArr as $col)
			{
				if($col!="MSSV")
				{
					$query=$query.$col.",";
				}
				else
				{
					$query=$query."SINHVIEN.MSSV".",";
				}
			}
			$query = substr($query,0,strlen($query)-1);
			$query = $query." From $table,SINHVIEN where ";
			for($i=0;$i<count($checkboxArr);$i++)
			{
				if($checkboxArr[$i]!="MSSV")
				{
					$query=$query.$checkboxArr[$i]." like '%$selectedArr[$i]%' and ";
				}
				else
				{
					$query=$query."SINHVIEN.MSSV"." like '%$selectedArr[$i]%' and ";
				}
			}
			$query = $query."SINHVIEN.MSSV = $table.MSSV";
		}
		else if($table=="KETQUA")
		{
			foreach($checkedArr as $col)
			{
				if($col=="MAKHOA")
				{
					$query=$query."SINHVIEN.MAKHOA".",";
				}
				else if($col=="MSSV")
				{
					$query=$query."SINHVIEN.MSSV".",";
				}
				else
				{
					$query=$query.$col.",";
				}
			}
			$query = substr($query,0,strlen($query)-1);
			$query = $query." From $table,SINHVIEN,KHOA where ";
			for($i=0;$i<count($checkboxArr);$i++)
			{
				if($checkboxArr[$i]=="MAKHOA")
				{
					$query=$query."SINHVIEN.MAKHOA"." like '%$selectedArr[$i]%' and ";
				}
				else if($checkboxArr[$i]=="MSSV")
				{
					$query=$query."SINHVIEN.MSSV"." like '%$selectedArr[$i]%' and ";
				}
				else
				{
					$query=$query.$checkboxArr[$i]." like '%$selectedArr[$i]%' and ";
				}
			}
			$query = $query."SINHVIEN.MSSV = $table.MSSV and SINHVIEN.MAKHOA = KHOA.MAKHOA";
		}
		else if($table=="QUANTRIVIEN"||$table=="HOCKY"||$table=="KHOA")
		{
			foreach($checkedArr as $col)
			{
				$query=$query.$col.",";
			}
			$query = substr($query,0,strlen($query)-1);
			$query = $query." From $table where ";
			for($i=0;$i<count($checkboxArr);$i++)
			{
				$query=$query.$checkboxArr[$i]." like '%$selectedArr[$i]%' and ";
			}
			$query = substr($query,0,strlen($query)-5);
		}
		return $query;
	}
	public function returnFilter($table,$checkboxArr,$checkedArr,$selectedArr)
	{
		$query =  self::returnQuery($table,$checkboxArr,$checkedArr,$selectedArr);
		//echo $query;
		//echo $query."<br>";
		$res = mysql_query($query);
		echo "<table>";
		echo "<tr>";
		while($colTit = mysql_fetch_field($res))
		{
			echo "<td>";
			echo self::convert($colTit->name);
			echo "</td>";
		}
		echo "</tr>";
		while($rows = mysql_fetch_assoc($res))
		{
			echo "<tr>";
			foreach($rows as $valu)
			{
				echo "<td>$valu</td>";
			}
			echo "</tr>";
		}
		echo "</table>";
		
	}
}
?>

